class count{
    public static int num=0;
    public  int a=0;
       synchronized  static void add () {
            num++;
        }
        synchronized public  void add2(){
           synchronized (this){
               a++;
           }
        }
}
public class ThreadDemo1 {
    //原子性
    public static void main(String[] args) {
        count count=new count();
        Thread t=new Thread(()->{
            for (int i = 0; i <1000 ; i++) {
                count.add2();
            }
        });
        Thread t1=new Thread(()->{
            for (int i = 0; i <1000 ; i++) {
                count.add2();
            }
        });
        t.start();
        t1.start();
        try {
            t.join();
            t1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(count.a);
    }
}
